package com.course.server.service;/**
 * @author: lin
 */

import com.course.server.domain.User;
import com.course.server.domain.UserExample;
import com.course.server.dto.PageDto;
import com.course.server.dto.UserDto;
import com.course.server.exception.BusinessException;
import com.course.server.exception.BusinessExceptionCode;
import com.course.server.mapper.UserMapper;
import com.course.server.util.CopyUtil;
import com.course.server.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
/**
 *@Author lin
 *@Data Created in 2020/7/28 21:21
 *Description 
 *@Modified By
 *@Version V1.0
 */
@Service
public class UserService {
    @Resource
    private UserMapper userMapper;
    /**
     * create by: lin
     * description: 列表查询
     * create time: 2020/8/2 17:40
     */
    public void list(PageDto pageDto){
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
        UserExample userExample = new UserExample();
        List<User> userList = userMapper.selectByExample(userExample);
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        pageDto.setTotal(pageInfo.getTotal());
        List<UserDto> userDtoList = CopyUtil.copyList(userList, UserDto.class);
        pageDto.setList(userDtoList);
    }
    /**
     * create by: lin
     * description: 保存
     * create time: 2020/8/2 17:40
     */
    public void save(UserDto userDto){
        User user = CopyUtil.copy(userDto, User.class);
        if(StringUtils.isEmpty(userDto.getId())){
            //StringUtils.isEmpty spring的工具类来判断是否有值
            this.insert(user);
        }else {
            this.update(user);
        }
    }
    /**
     * create by: lin
     * description: 新增
     * create time: 2020/8/2 17:41
     */
    private void insert(User user) {
        user.setId(UuidUtil.getShortUuid());
        User userDb = this.selectByLoginName(user.getLoginName());
        if (userDb != null) {
            throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
        }
        userMapper.insert(user);
    }

    /**
     * create by: lin
     * description: 修改
     * create time: 2020/8/2 17:58
     */
    private void update(User user){
        userMapper.updateByPrimaryKey(user);
    }
    /**
     * create by: lin
     * description: 删除
     * create time: 2020/8/2 17:41
     */
    public void delete(String id){
        userMapper.deleteByPrimaryKey(id);
    }
    /**
     * create by: lin
     * description: 根据登录名查询用户信息
     * create time: 2020/8/2 17:41
     */
    public User selectByLoginName(String loginName) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andLoginNameEqualTo(loginName);
        List<User> userList = userMapper.selectByExample(userExample);
        if (CollectionUtils.isEmpty(userList)) {
            return null;
        } else {
            return userList.get(0);
        }
    }
}
